package wtkx.ps;

import ps.Request;
import ps.Output;

import com.google.protobuf.ByteString;
import com.google.protobuf.Message;

import java.io.IOException;
import java.net.URL;


public class Automaton
    extends ps.q.Queue
{


    public Automaton(Object id){
        super(id);
    }


    public final Message newOpenByPath(String id, String path){
        if (null != id && null != path){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.OpenByPath);
            ctor.setId(id);
            ctor.setPath(path);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendOpenByPath(String id, String path){
        this.update(this.newOpenByPath(id,path));
    }
    public final Message newOpenByUrl(String id, URL url){
        if (null != id && null != url){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.OpenByUrl);
            ctor.setId(id);
            ctor.setUrl(url.toExternalForm());
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendOpenByUrl(String id, URL url){
        this.update(this.newOpenByUrl(id,url));
    }
    public final Message newOpenWithContent(String id, byte[] content){
        if (null != id && null != content){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.OpenWithContent);
            ctor.setId(id);
            ctor.setContent(ByteString.copyFrom(content,0,content.length));
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendOpenWithContent(String id, byte[] content){
        this.update(this.newOpenWithContent(id,content));
    }
    public final Message newUpdateAppend(String id, String at, String with){
        if (null != id && null != at && null != with){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.UpdateAppend);
            ctor.setId(id);
            ctor.setAt(at);
            ctor.setWith(with);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendUpdateAppend(String id, String at, String with){
        this.update(this.newUpdateAppend(id,at,with));
    }
    public final Message newUpdateInsert(String id, String at, int start, String with){
        if (null != id && null != at && -1 < start && null != with){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.UpdateInsert);
            ctor.setId(id);
            ctor.setAt(at);
            ctor.setStart(start);
            ctor.setWith(with);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendUpdateInsert(String id, String at, int start, String with){
        this.update(this.newUpdateInsert(id,at,start,with));
    }
    public final Message newUpdateReplace(String id, String at, String search, String with){
        if (null != id && null != at && null != search && null != with){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.UpdateReplace);
            ctor.setId(id);
            ctor.setAt(at);
            ctor.setSearch(search);
            ctor.setWith(with);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendUpdateReplace(String id, String at, String search, String with){
        this.update(this.newUpdateReplace(id,at,search,with));
    }
    public final Message newUpdateDelete(String id, String at, int start, int end){
        if (null != id && null != at && -1 < start && -1 < end){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.UpdateDelete);
            ctor.setId(id);
            ctor.setAt(at);
            ctor.setStart(start);
            ctor.setEnd(end);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendUpdateDelete(String id, String at, int start, int end){
        this.update(this.newUpdateDelete(id,at,start,end));
    }
    public final Message newClose(String id){
        if (null != id){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.Close);
            ctor.setId(id);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendClose(String id){
        this.update(this.newClose(id));
    }
    public final Message newHeartbeat(String id){
        if (null != id){
            Wtkx.Update.Builder ctor = Wtkx.Update.newBuilder();
            ctor.setOp(Wtkx.Op.Heartbeat);
            ctor.setId(id);
            return ctor.build();
        }
        else
            throw new IllegalArgumentException();
    }
    public final void sendHeartbeat(String id){
        this.update(this.newHeartbeat(id));
    }
}
